import 'package:base/main.dart';
import 'package:flutter/material.dart';
import 'package:framework/core/base_logic.dart';

abstract class BaseView<L extends BaseLogic> extends StatefulWidget {
  const BaseView({super.key});

  @override
  State<BaseView<L>> createState() => _BaseViewState<L>();

  L createLogic();

  // 子类需实现的 UI 构建方法
  Widget buildView(BuildContext context, L logic);

  initState() {}

  dispose() {}
}

class _BaseViewState<L extends BaseLogic> extends State<BaseView<L>> {
  late L logic;

  @override
  void initState() {
    if (!Get.isRegistered<L>()) {
      Log.w("BaseView 注册 ${widget.createLogic()} ");
      logic = Get.put(widget.createLogic());
    }
    logic.ready();
    widget.initState();
    super.initState();
  }

  @override
  void dispose() {
    if (Get.isRegistered<L>()) {
      Get.delete<L>();
    }
    widget.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        widget.buildView(context, logic),
        Obx(() => _buildStatusOverlay()),
      ],
    );
  }

  Widget _buildStatusOverlay() {
    if (logic.isLoading) {
      return const Center(child: CircularProgressIndicator());
    }
    return const SizedBox();
  }
}
